import {Message} from 'element-ui';
import {codeMap} from './env';

/**
 * 获取成功之后的数据
 */
export const parsed = (data) => {
  if (data.statusCode === 200) {
    return data.message;
  } else {
    const statusCode = data.message;
    Message({
      showClose: true,
      type: 'warning',
      message: codeMap[statusCode]
    });
    throw new Error(statusCode);
  }
};

/**
 * 存储localStorage
 */
export const setStore = (name, content) => {
  if (!name) return;
  if (typeof content !== 'string') {
    content = JSON.stringify(content);
  }
  window.localStorage.setItem(name, content);
};

/**
 * 获取localStorage
 */
export const getStore = name => {
  if (!name) return;
  return window.localStorage.getItem(name);
};

/**
 * 删除localStorage
 */
export const removeStore = name => {
  if (!name) return;
  window.localStorage.removeItem(name);
};
